package com.stox.ui.javafx.charting.model.uiunit;

import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

import com.stox.indicator.MACD.Unit;
import com.stox.ui.javafx.charting.model.plot.MACDPlot;
import com.stox.ui.javafx.charting.widget.Chart;

public class MACDUiUnit extends AbstractUiUnit<Unit, Chart<MACDPlot>, MACDUiUnit, MACDPlot> {

	private double macdY;
	private double signalY;
	private double histY;
	private double zeroY;
	
	public MACDUiUnit(Unit model, MACDPlot plot, int index) {
		super(model, plot, index);
	}

	@Override
	public void update() {
		super.update();
		if(null == getModel()) return;
		macdY = height(getModel().getMacd());
		signalY = height(getModel().getSignal());
		histY = height(getModel().getHist());
		zeroY = height(0);
	}
	
	@Override
	public void paint() {
		if(getIndex() >= getPlot().getChart().getChartingView().getEndIndex()-1){
			return;
		}
		
		Canvas canvas = getPlot().getChart().getCanvas();
		GraphicsContext gc = canvas.getGraphicsContext2D();
		gc.setFill(Color.BLUE);
		double w = width*3/4;
		gc.fillRoundRect(x+width/8, Math.min(histY, zeroY), w, Math.abs(histY-zeroY), w, w);
		
		MACDUiUnit previousUiUnit = getPlot().getUiUnits().get(getIndex()+1);
		if(null == previousUiUnit) return;
		double previousX = previousUiUnit.getX() + previousUiUnit.getWidth()/2;
		double currentX = x + width/2;
		gc.setLineWidth(1);
		gc.setStroke(Color.GREEN);
		gc.strokeLine(previousX, previousUiUnit.getSignalY(), currentX, getSignalY());
		gc.setStroke(Color.RED);
		gc.strokeLine(previousX, previousUiUnit.getMacdY(), currentX, getMacdY());
	}

	public double getMacdY() {
		return macdY;
	}

	public double getSignalY() {
		return signalY;
	}

	public double getHistY() {
		return histY;
	}

	public double getZeroY() {
		return zeroY;
	}
}
